package com.wolfalpha.utron.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormal2Activity;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.UserInfo;
import com.wolfalpha.utron.ui.UIHelper;


/**
 * 查看用户信息界面
 * <p/>
 * Created by luodon on 15/8/21.
 */
public class UserInfoDetailActivity extends BaseNormal2Activity {

    private TextView tvSex;    //性别

    private TextView tvNick;    //昵称
    private TextView tvAge;     //年龄
    private TextView tvSchool;  //学校
    private TextView tvMail;    //邮箱
    private TextView tvHome;    //故乡
    private TextView tvSign;    //签名
    private TextView tvIntro;   //个人简介

    private UserInfo userInfo;  //用户信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info_detail);
        Bundle bundle = getIntent().getExtras();
        if (bundle == null || !bundle.containsKey("userInfo")) {
            UIHelper.ToastMessage(this, "信息初始化失败");
            finish();
        }
        userInfo = (UserInfo) bundle.getSerializable("userInfo");
    }

    @Override
    public void onResume() {
        super.onResume();
        initView();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        if (userInfo.getUser_id() == AppContext.getInstance().getUserId()) {
            setTvTitle("我的资料");
            setRightIcon(R.mipmap.app_write);
            setRightVisiable(true);

            userInfo = AppContext.getInstance().getUserInfo();
        } else {
            setTvTitle("基本信息");
            setRightVisiable(false);
        }

        tvSex = (TextView) findViewById(R.id.tv_user_info_detail_sex);
        tvNick = (TextView) findViewById(R.id.tv_user_info_detail_nickname);
        tvAge = (TextView) findViewById(R.id.tv_user_info_detail_age);
        tvSchool = (TextView) findViewById(R.id.tv_user_info_detail_school);
        tvMail = (TextView) findViewById(R.id.tv_user_info_detail_mail);
        tvHome = (TextView) findViewById(R.id.tv_user_info_detail_home);
        tvSign = (TextView) findViewById(R.id.tv_user_info_detail_sign);
        tvIntro = (TextView) findViewById(R.id.tv_user_info_detail_intro);

        tvSex.setText(userInfo.getSex() == 1 ? "女" : userInfo.getSex() == 2 ? "男" : "保密");

        final String[] arr = this.getResources().getStringArray(R.array.user_age_years);
        int age = userInfo.getAge();
        if (age < 0 || age > arr.length) {
            tvAge.setText("未知");
        } else {
            tvAge.setText(arr[age]);
            if (age == 0 || age == arr.length - 1) {
                tvAge.setText(arr[0]);
            } else {
                tvAge.setText(31 - age + "");
            }
        }

        if (userInfo.getNickname() != null) {
            tvNick.setText(userInfo.getNickname());
        }
        if (userInfo.getSchool() != null) {
            tvSchool.setText(String.valueOf(userInfo.getSchool()));
        }
        if (userInfo.getMail() != null) {
            tvMail.setText(String.valueOf(userInfo.getMail()));
        }
        if (userInfo.getHome() != null) {
            tvHome.setText(userInfo.getHome());
        }
        if (userInfo.getUtron_sign() != null) {
            tvSign.setText(String.valueOf(userInfo.getUtron_sign()));
        }
        if (userInfo.getIntroduction() != null) {
            tvIntro.setText(String.valueOf(userInfo.getIntroduction()));
        }

    }

    @Override
    public void rightOperation() {
        UIHelper.showActivity(UserInfoDetailActivity.this, UserEditActivity.class);
    }
}
